<?php

namespace App\Http\Requests;

use App\Models\StorageConfig;
use Illuminate\Foundation\Http\FormRequest;

class StoreStorageConfigRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $type = $this->input('type');
        
        $rules = [
            'type' => 'required|string|in:' . implode(',', [
                StorageConfig::TYPE_LOCAL,
                StorageConfig::TYPE_ALIYUN_OSS,
                StorageConfig::TYPE_TENCENT_COS,
                StorageConfig::TYPE_QINIU,
            ]),
            'name' => 'required|string|max:100',
            'is_default' => 'nullable|boolean',
            'status' => 'required|integer|in:' . implode(',', [
                StorageConfig::STATUS_DISABLED,
                StorageConfig::STATUS_ENABLED,
            ]),
            'remark' => 'nullable|string',
        ];

        // 根据类型添加配置验证规则
        $configRules = $this->getConfigRulesByType($type);
        $rules['config'] = ['required', 'array'];
        foreach ($configRules as $key => $rule) {
            $rules["config.{$key}"] = $rule;
        }

        return $rules;
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages(): array
    {
        return [
            'type.required' => '存储类型不能为空',
            'type.in' => '不支持的存储类型',
            'name.required' => '配置名称不能为空',
            'config.required' => '存储配置不能为空',
            'config.array' => '存储配置必须是数组',
        ];
    }

    /**
     * 根据类型获取配置验证规则
     *
     * @param string $type
     * @return array
     */
    protected function getConfigRulesByType(?string $type): array
    {
        if (!$type) {
            return [];
        }

        $rules = [];

        switch ($type) {
            case StorageConfig::TYPE_LOCAL:
                $rules = [
                    'root' => 'nullable|string',
                    'url' => 'nullable|string',
                ];
                break;

            case StorageConfig::TYPE_ALIYUN_OSS:
                $rules = [
                    'access_key_id' => 'required|string',
                    'access_key_secret' => 'required|string',
                    'bucket' => 'required|string',
                    'endpoint' => 'required|string',
                    'domain' => 'nullable|string',
                    'is_cname' => 'nullable|boolean',
                ];
                break;

            case StorageConfig::TYPE_TENCENT_COS:
                $rules = [
                    'secret_id' => 'required|string',
                    'secret_key' => 'required|string',
                    'region' => 'required|string',
                    'bucket' => 'required|string',
                    'domain' => 'nullable|string',
                ];
                break;

            case StorageConfig::TYPE_QINIU:
                $rules = [
                    'access_key' => 'required|string',
                    'secret_key' => 'required|string',
                    'bucket' => 'required|string',
                    'domain' => 'nullable|string',
                ];
                break;
        }

        return $rules;
    }
}

